﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class SysUser
    {
        private readonly Chain.IDAL.SysUser dal = new Chain.IDAL.SysUser();

        public int Add(Chain.Model.SysUser model)
        {
            if (this.Exists(model.UserAccount))
            {
                return -1;
            }
            if (this.ExiNumber(model.UserNumber))
            {
                return -4;
            }
            return this.dal.Add(model);
        }

        public Chain.Model.SysUser CheckUserLogin(string UserAccount, string Md5Pwd)
        {
            DataTable dt = this.dal.CheckUserLogin(UserAccount, Md5Pwd);
            if (dt.Rows.Count > 0)
            {
                return this.DataTableToList(dt)[0];
            }
            return null;
        }

        public List<Chain.Model.SysUser> DataTableToList(DataTable dt)
        {
            List<Chain.Model.SysUser> list = new List<Chain.Model.SysUser>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.SysUser item = new Chain.Model.SysUser();
                    if ((dt.Rows[i]["UserID"] != null) && (dt.Rows[i]["UserID"].ToString() != ""))
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    if ((dt.Rows[i]["UserAccount"] != null) && (dt.Rows[i]["UserAccount"].ToString() != ""))
                    {
                        item.UserAccount = dt.Rows[i]["UserAccount"].ToString();
                    }
                    if ((dt.Rows[i]["UserName"] != null) && (dt.Rows[i]["UserName"].ToString() != ""))
                    {
                        item.UserName = dt.Rows[i]["UserName"].ToString();
                    }
                    if ((dt.Rows[i]["UserPassword"] != null) && (dt.Rows[i]["UserPassword"].ToString() != ""))
                    {
                        item.UserPassword = dt.Rows[i]["UserPassword"].ToString();
                    }
                    if ((dt.Rows[i]["UserShopID"] != null) && (dt.Rows[i]["UserShopID"].ToString() != ""))
                    {
                        item.UserShopID = int.Parse(dt.Rows[i]["UserShopID"].ToString());
                    }
                    if ((dt.Rows[i]["UserGroupID"] != null) && (dt.Rows[i]["UserGroupID"].ToString() != ""))
                    {
                        item.UserGroupID = int.Parse(dt.Rows[i]["UserGroupID"].ToString());
                    }
                    if ((dt.Rows[i]["UserLock"] != null) && (dt.Rows[i]["UserLock"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["UserLock"].ToString() == "1") || (dt.Rows[i]["UserLock"].ToString().ToLower() == "true"))
                        {
                            item.UserLock = true;
                        }
                        else
                        {
                            item.UserLock = false;
                        }
                    }
                    if ((dt.Rows[i]["UserRemark"] != null) && (dt.Rows[i]["UserRemark"].ToString() != ""))
                    {
                        item.UserPassword = dt.Rows[i]["UserRemark"].ToString();
                    }
                    if ((dt.Rows[i]["UserCreateTime"] != null) && (dt.Rows[i]["UserCreateTime"].ToString() != ""))
                    {
                        item.UserCreateTime = DateTime.Parse(dt.Rows[i]["UserCreateTime"].ToString());
                    }
                    if ((dt.Rows[i]["UserTelephone"] != null) && (dt.Rows[i]["UserTelephone"].ToString() != ""))
                    {
                        item.UserTelephone = dt.Rows[i]["UserTelephone"].ToString();
                    }
                    if ((dt.Rows[i]["UserNumber"] != null) && (dt.Rows[i]["UserNumber"].ToString() != ""))
                    {
                        item.UserNumber = dt.Rows[i]["UserNumber"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int UserID)
        {
            return this.dal.Delete(UserID);
        }

        public bool DeleteList(string UserIDlist)
        {
            return this.dal.DeleteList(UserIDlist);
        }

        public bool ExiNumber(string usernumber)
        {
            return this.dal.ExiNumber(usernumber);
        }

        public int ExistPwd(int UserID, string oldPwd)
        {
            return this.dal.ExistPwd(UserID, oldPwd);
        }

        public bool Exists(string useraccount)
        {
            return this.dal.Exists(useraccount);
        }

        public bool Exists(string useraccount, int userid)
        {
            return this.dal.Exists(useraccount, userid);
        }

        public bool Exists(string useraccount, string usernumber)
        {
            return this.dal.Exists(useraccount, usernumber);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetIsCanDelUser(int userid)
        {
            return this.dal.GetIsCanDelUser(userid);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string[] where, out int resCount)
        {
            return this.dal.GetList(PageSize, PageIndex, where, out resCount);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.SysUser GetModel(int UserID)
        {
            return this.dal.GetModel(UserID);
        }

        public Chain.Model.SysUser GetModel(string Account)
        {
            return this.dal.GetModel(Account);
        }

        public List<Chain.Model.SysUser> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public string GetUserNameByUserID(string userid)
        {
            return this.dal.GetUserNameByUserID(userid);
        }

        public int Update(Chain.Model.SysUser model)
        {
            if (this.Exists(model.UserAccount, model.UserID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }

        public int UpdateUserLock(int ShopID, int type)
        {
            return this.dal.UpdateUserLock(ShopID, type);
        }

        public int UpdateUserPwd(int UserID, string oldPwd, string newPwd)
        {
            int num = this.ExistPwd(UserID, oldPwd);
            if (num != 1)
            {
                return num;
            }
            return this.dal.UpdateUserPwd(UserID, newPwd);
        }
    }
}

